[TT-16980] Fix pgx v5 breaking change when postgres.prefer_simple_protocol is set to true#969
Conversation
…tocol is set to true
|
This PR addresses a breaking change introduced by the The fix intercepts the database connection process for the PostgreSQL dialect. Instead of letting the GORM driver create the connection, this change manually constructs a To validate the solution, a new test ( Files Changed Analysis
Architecture & Impact Assessment
Connection and Encoding FlowsequenceDiagram
participant Pump as "Tyk Pump (sql.go)"
participant GORM as "gorm.io/gorm"
participant PGX as "github.com/jackc/pgx/v5"
participant PG as "PostgreSQL DB"
Pump->>Pump: Dialect("postgres") called
Pump->>PGX: pgx.ParseConfig(dsn)
Pump->>PGX: stdlib.OpenDB(pgxConfig, OptionAfterConnect)
Note right of Pump: AfterConnect callback is configured
PGX->>PG: Establishes new connection
PG-->>PGX: Connection successful
PGX->>Pump: Executes AfterConnect callback
Pump->>PGX: Prepend custom encoder for time.Month
PGX-->>Pump: Returns *sql.DB
Pump->>GORM: postgres.New(Config{Conn: sqlDB})
Note over Pump, PG: Later, when writing data...
GORM->>PGX: Executes query with time.Month
PGX->>PGX: Custom encoder converts time.Month to int
PGX->>PG: Sends SQL with integer value for month
Scope Discovery & Context Expansion
Metadata
Powered by Visor from Probelabs Last updated: 2026-04-17T08:33:29.697Z | Triggered by: pr_opened | Commit: 116d4c2 💡 TIP: You can chat with Visor using |
✅ Security Check PassedNo security issues found – changes LGTM. ✅ Architecture Check PassedNo architecture issues found – changes LGTM. ✅ Performance Check PassedNo performance issues found – changes LGTM. Powered by Visor from Probelabs Last updated: 2026-04-17T08:31:44.452Z | Triggered by: pr_opened | Commit: 116d4c2 💡 TIP: You can chat with Visor using |
|
…tocol is set to true (#969)
* fix CVE-2026-32286 * update to pgx v5.9.1 * test gorm fork update * fix CI * tests for gorm update * fix mysql test * more tests * skip TestPreferSimpleProtocol * [TT-16980] Fix pgx v5 breaking change when postgres.prefer_simple_protocol is set to true (#969) --------- Co-authored-by: Burak Sezer <burak.sezer.developer@gmail.com> Co-authored-by: Leonid Bugaev <leonsbox@gmail.com>


PR for https://tyktech.atlassian.net/browse/TT-16980
The proposed solution(
BeforeCreate+SetColumn) didn't work because GORM'sConvertToCreateValuesreads field values viafield.ValueOf(), which uses reflection to returnfieldValue.Interface(). Since the struct field type istime.Month, the value always reaches pgx astime.Monthregardless of whatSetColumnwrote. Go's reflection preserves the named type.Another possible solution might be to change the data type of
Monthfield inAnalyticsRecord. The current type istime.Month, if we create a new new type that implementsdriver.Valuerbut it is a potential breaking change and requires update across all Tyk components other potentials dependents.Implemented solution: In
pumps/sql.go, I replaced the postgres case inDialect()to build the*sql.DBourselves viastdlib.OpenDBwith anOptionAfterConnectcallback.On each new connection, we prepend a custom
TryWrapEncodePlanFuncto the encode plan chain that interceptstime.Monthand converts it to int before pgx'sfmt.Stringerwrapper can fire. I also replicate the timezone extraction logic fromgorm.io/driver/postgresto avoid behavioral regression, and pass the pre-built*sql.DBto GORM viapostgres.Config{Conn: sqlDB}.I have also added a test named
TestSQLWriteData_PreferSimpleProtocol_Month, before the fix that test was failing.Skip is also removed from
TestPreferSimpleProtocol_Postgres.How to run tests: